<?php

if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class Acd_servers extends MY_Controller {

	function __construct() {
		parent::__construct();
		$this->require_login();
		$this->load->model('acd_server_model', 'acd_server');
	}

	function index() 
	{
		check_permission("acd_servers", "view");
		$GET = $this->input->get(NULL, true);
 		
		$config = array(
						"id" 			=> array("dataFieldHeading" => "ID", "dataFieldType" => "text", "hide_on_grid" => 1),
						"server_ip" 	=> array("dataFieldHeading" => "Server IP", "dataFieldType" => "text"),
						"status" 		=> array("dataFieldHeading" => "Status", "cell_attributes" => " width='10%' style='text-align:center'" ),
						"edit"	=> array(	
											"link" => 1,
											"dataFieldHeading" => "Edit", 
											"skip_db_select" => 1,
											"url" => "acd_servers/save/{id}",
											"link_image" => "edit.png",
											"link_caption" => "edit",
											"cell_attributes" => " width='10%' style='text-align:center' "
										),
						"delete" => array(	
											"link" => 1,
											"dataFieldHeading" => "Delete", 
											"skip_db_select" => 1,
											"url" => "acd_servers/delete/{id}",
											"link_image" => "del_opt.gif",
											"link_caption" => "delete",
											"link_attributes" => "",
											"cell_attributes" => " width='10%' style='text-align:center' "
										)								
        			);
		
        $data["dataFields"] = $config;
		
		$data["dataOthers"] = array(	
									"addLink" => array("url" => $this->uri->segment(1)."/save", "caption" => "Add ACD Server"),
									"page_heading" => "ACD Server",
                                    "controller_base_path" => $this->uri->segment(1)."/index",
									"hide_search_colums" => 1,
									"hide_record_detail" => 1,
									"show_serial_no" => 1,
									"searchPanelView" => $this->uri->segment(1).'/search_box'
									);
									
 		$data['customWhere'] = " ";
		$sql_without_select = " FROM ACD_servers ";
		
		if( ! check_module('acd_servers', 'add') ) { unset($data["dataOthers"]["addLink"]); 	}
		if( ! check_module('acd_servers', 'edit') ) { unset($data["dataFields"]["edit"]); 		}
		if( ! check_module('acd_servers', 'delete') ) { unset($data["dataFields"]["delete"]); 	}
				
		showGridListingData($sql_without_select, $data);
	
	}

	function delete($id = 0)
	{
		check_permission("acd_servers", "delete");
		
		if( $this->acd_server->delete($id) ) {
			$this->session->set_flashdata('userMesg', 'Record has been deleted successfully.');
		} else	{
			$this->session->set_flashdata('errMsg', 'Record can not be deleted. It have association with Skill, please remove it from Skill Mapping First.');
		}
		redirect('acd_servers/');
	}

	function save($id = 0) 
	{
		$id = (int)$id;
		$data = array();
		
		if( $id > 0 ) {
			check_permission("acd_servers", "edit");
		} else {
			check_permission("acd_servers", "add");
		}
		
		$this->form_validation->set_rules('server_ip', 'Server IP', 'trim|required|xss_clean|valid_ip|callback_check_duplicate['.$id.']');
		if ($this->form_validation->run() == FALSE)
		{
			if ( $id > 0)
			{
				$detail = $this->acd_server->getDetailByID( $id );
				foreach($detail as $k=>$v):
				$data[$k] = $v;
				endforeach;
				$data['id'] = $id;
			}
		
			$this->load->view('acd_servers/save', $data);
		} 
		else 
		{
			$record = array( 'server_ip' => addslashes($this->input->post('server_ip')) );	
			if ( $id > 0 )
			{
				$record['id'] = $id;
				$this->acd_server->save($record);
				$this->session->set_flashdata('userMesg', 'Record has been updated successfully.');
			}
			else
			{
				$this->acd_server->save($record);
				$this->session->set_flashdata('userMesg', 'Record has been added successfully.');
			}
			redirect('acd_servers/');
		}
	}// end save
  
 	function check_duplicate($server_ip, $id ="")
    {
		$id = (int)$id;
		$where = " server_ip  = '".$server_ip."' ";
		if( $id > 0 ) {
			$where .= " AND id <> ".$id	;
		}
 		if(! $this->common_model->record_exist("ACD_servers", $where))
		{
			return true;	
		}
		else
		{
			$this->form_validation->set_message('check_duplicate', 'The %s Must be Unique.');
			return false;
		}
    }

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */